package cn.exrick.xboot.modules.graphics.controller;

import cn.exrick.xboot.common.constant.SecurityConstant;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.graphics.entity.Container;
import cn.exrick.xboot.modules.graphics.entity.GraphicsPage;
import cn.exrick.xboot.modules.graphics.param.ContainerListParam;
import cn.exrick.xboot.modules.graphics.param.ContainerParam;
import cn.exrick.xboot.modules.graphics.service.IContainerService;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author njp
 */
@Slf4j
@RestController
@Api(description = "容器管理接口")
@RequestMapping("/xboot/container")
@Transactional
public class ContainerController {

    @Autowired
    private IContainerService iContainerService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<Container> get(@PathVariable String id){

        Container container = iContainerService.getById(id);
        return new ResultUtil<Container>().setData(container);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<Container>> getAll(){

        List<Container> list = iContainerService.list();
        return new ResultUtil<List<Container>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<Container>> getByPage(@ModelAttribute PageVo page){

        IPage<Container> data = iContainerService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Container>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<Container> saveOrUpdate(@ModelAttribute Container container){
        container.setCreateTime(new Date());
        container.setUpdateTime(new Date());
        if(iContainerService.saveOrUpdate(container)){
            return new ResultUtil<Container>().setData(container);
        }
        return new ResultUtil<Container>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            iContainerService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
    @RequestMapping(value = "/delete/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delete(@PathVariable String[] ids){

        for(String id : ids){
            iContainerService.delete(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }

    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value = "容器分页查询")
    public Result<IPage<Container>> page(ContainerParam containerParam, PageVo page){
        IPage<Container> page1 = iContainerService.page(containerParam, PageUtil.initMpPage(page));
        return new ResultUtil<IPage<Container>>().setData(page1);
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value = "容器list查询")
    public Result<List<Container>> list(ContainerParam containerParam){
        List<Container> list = iContainerService.getList(containerParam);
        return new ResultUtil<List<Container>>().setData(list);
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "新增容器")
    public Result<Object>  add(@Validated(ContainerParam.add.class) ContainerParam containerParam){
        iContainerService.add(containerParam);
        return new ResultUtil<Object>().setSuccessMsg("新增成功");
    }

    @RequestMapping(value = "/init", method = RequestMethod.POST)
    @ApiOperation(value = "容器初始化")
    public Result<Object>  init(@RequestBody ContainerListParam list, HttpServletRequest request){
        //添加验证码验证（先采用固定验证码）（后续可以考虑给超级管理员发送短信验证的形式）
        String header = request.getHeader("ContainerInit");
        if("jiuqiInit".equals(header)){
        if(ObjectUtil.isNotEmpty(list)){
            System.out.println(list.toString());
            iContainerService.init(list);
        }
        return new ResultUtil<Object>().setSuccessMsg("初始化容器成功");
        }else {
            return new ResultUtil<Object>().setErrorMsg("没有初始化权限，请联系我司技术人员");
        }
    }

}
